package com.aiyige.page.mediaslider;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.aiyige.R;
import com.aiyige.base.eventbus.EventPhotoTap;
import com.aiyige.page.mediaslider.model.EventPageSelectChange;
import com.aiyige.page.mediaslider.model.Media;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.BitmapTransitionOptions;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.ViewTarget;
import com.bumptech.glide.request.transition.Transition;
import com.davemorrissey.labs.subscaleview.ImageSource;
import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;


public class PhotoSliderItemPage extends Fragment implements SelectablePage {
    EventBus eventBus;
    Media media = null;
    @BindView(R.id.photoView)
    SubsamplingScaleImageView photoView;
    Unbinder unbinder;

    public PhotoSliderItemPage() {
        eventBus = new EventBus();
    }

    // TODO: Rename and change types and number of parameters
    public static PhotoSliderItemPage newInstance(Media media) {
        PhotoSliderItemPage fragment = new PhotoSliderItemPage();
        fragment.setMedia(media);
        return fragment;
    }

    public Media getMedia() {
        return media;
    }

    public void setMedia(Media media) {
        this.media = media;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.page_photo_slider_item, container, false);
        unbinder = ButterKnife.bind(this, view);
        Glide.with(getContext())
                .asBitmap()
                .load(media == null || TextUtils.isEmpty(media.getImageUrl()) ? "" : media.getImageUrl())
                .apply(RequestOptions.placeholderOf(android.R.color.transparent))
                .transition(BitmapTransitionOptions.withCrossFade())
                .into(new ViewTarget<SubsamplingScaleImageView, Bitmap>(photoView) {
                    @Override
                    public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {
                        this.view.setImage(ImageSource.bitmap(resource));
                    }
                });


        try {
            eventBus.register(this);
        } catch (Exception e) {

        }
        return view;
    }


    @Override
    public void onDestroyView() {
        try {
            eventBus.unregister(this);
        } catch (Exception e) {

        }
        super.onDestroyView();
        unbinder.unbind();
    }

    @Subscribe(sticky = true, threadMode = ThreadMode.MAIN)
    public void onEvent(EventPageSelectChange event) {
        if (event.isSelected()) {

        }
    }

    @Override
    public void onSelectChanged(boolean value) {
        eventBus.postSticky(EventPageSelectChange.newBuilder().selected(value).build());
    }
}
